<?php
require COMMON_PATH.'crypt.php';

class UserModel extends Model {
    protected $fields=array(
        'account', 'password', 'realname', 'alias', 'created',
        '_pk'=>'account',
        '_autoInc'=>false
    );

    protected $_auto=array(
        array('account','trim','ALL','function'),
        array('realname','trim','ALL','function'),
        array('password','cryptUserPassword','ALL','function')
    );

    protected $_validate=array(
        array('role','/^[1-9]\d*$/','所属角色填写不正确'),
        array('account','/^[a-zA-Z][a-zA-Z0-9_@-]{0,15}$/','用户名填写不正确',1),
        array('password','/^.{6,}$/','密码长度必须在６位以上',2),
        array('realname','require','真实姓名填写不正确')
    );

    /**
     * 添加用户，成功返回用户信息，失败返回false
     */
    public function addUser(){
        $userVo=$this->create();
        if(!$userVo){
            return false;
        }
        $role=$_POST['role'];
        $checkRoleRs=$this->query('select * from '.$this->tablePrefix.'group where id='.$role);
        if(true===empty($checkRoleRs)){
            $this->error='角色信息不存在';
            return false;
        }
        $checkUserRs=$this->where('account=\''.$userVo['account'].'\'')->find();
        if(false===empty($checkUserRs)){
            $this->error='用户名<b>'.$userVo['account'].'</b>已经被使用';
            return false;
        }
        $userVo['created']=date('Y-m-d H:i:s');
        $this->startTrans();
        $userRs=$this->add($userVo);
        if($userRs){
            $groupUserDao=new GroupuserModel();
            if($groupUserDao->_add($userRs, $role)){
                $this->commit();
                $userRs['role']=$role;
                return $userVo;
            }else{
                $this->rollback();
            }
        }
        $this->error='保存数据到数据库发生了错误';
        return false;
    }

    /**
     * 用户登录，登录成功返回true,失败返回false
     */
    public function doLogin(){
        $userVo=$this->create();
        if(!$userVo){
            $this->error='获取人员信息失败';
            return false;
        }
        $userRs=$this->where("account='{$userVo['account']}'")->find();
        if(!$userRs){
            $this->error='用户<b>'.$userVo['account'].'</b>不存在';
            return false;
        }
        if($userRs['password']!==$userVo['password']){
            $this->error='密码不正确';
            return false;
        }
        RBAC::saveAccessList($userRs['role']);
        $eventDao=getEventDao();
        $latestLogin=$eventDao->findLastestEvent($userRs['account'],'LOGIN');
        if($latestLogin){
            $_SESSION[C('USER_AUTH_KEY')]=array_merge($userRs,$latestLogin);
        }else{
            $_SESSION[C('USER_AUTH_KEY')]=$userRs;
        }
        $notAuthRole=C('not_auth_role');
        if( is_array($notAuthRole) && in_array($userRs['role'],$notAuthRole) ){
            $_SESSION[C('ADMIN_AUTH_KEY')]=true;
        }
        $eventDao->logEvent($userRs['account'],'LOGIN',array(
                'DATE'=>NOW,
                'IP'=>get_client_ip(),
            ));
        return true;
    }
}
